package com.cqupt.service.serviceImp;

import com.cqupt.dao.*;
import com.cqupt.paging.Paging;
import com.cqupt.pojo.*;
import com.cqupt.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImp implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private  UserMapper userMapper;
    @Autowired
    private FeedbackMapper feedbackMapper;
    @Autowired
    private ComplaintMapper complaintMapper;
    @Autowired
    private PictureMapper pictureMapper;
    //查询商品
    @Override
    public List selectGoods(){
        return  goodsMapper.selectByExample(null);
    }
    //查询商品分页
    @Override
    public List selectPageGoods(Integer number){
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andIdBetween(6*(number-1),6*number);
        return goodsMapper.selectByExample(goodsExample);

    }
//调取图片
    @Override
public List getPicturePath(String page){
        PictureExample pictureExample = new PictureExample();

        pictureExample.createCriteria().andPicnameEqualTo(page);
        List<Picture> pictures = pictureMapper.selectByExample(pictureExample);
        List<String> path = new ArrayList<>();


        for (int i = 0; i < pictures.size(); i++) {
            path.add(pictures.get(i).getPicpath());
        }
        return path;



    }
    //下架商品
    @Override
    public int offlineGoods(Integer id) {
        GoodsExample goodsExample = new GoodsExample();
        Goods tbgoods = new Goods();
        goodsExample.createCriteria().andIdEqualTo(id);
        List<Goods> list = goodsMapper.selectByExample(goodsExample);
        if (list.size() > 0) {
            tbgoods = list.get(0);
        }
        tbgoods.setState(0);
        return goodsMapper.updateByPrimaryKeySelective(tbgoods);
    }
    //上架商品
    public  int onlineGoods(Integer id){
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andIdEqualTo(id);
        List<Goods> list = goodsMapper.selectByExample(goodsExample);
        Goods tbgoods = new Goods();
        if (list.size() > 0) {
            tbgoods = list.get(0);
        }
        tbgoods.setState(1);
        return goodsMapper.updateByPrimaryKeySelective(tbgoods);


    }


    //增加商品
    @Override
    public int insertGoods(Goods goods) {
        return goodsMapper.insertSelective(goods);
    }
    //更新商品
    @Override
    public int updateGoods(Goods goods) {
        Goods tbgoods = null;

        GoodsExample example = new GoodsExample();
        if (goods != null && goods.getId() != null) {
            example.createCriteria().andIdEqualTo(goods.getId());
        }
        List<Goods> list = goodsMapper.selectByExample(example);
        if (list.size() > 0) {
            tbgoods = list.get(0);
        }
        if(goods.getName() != null) {
            tbgoods.setName(goods.getName());
        }
        if(goods.getScore() != null){
            tbgoods.setScore(goods.getScore());
        }
        if (goods.getState() != null){
            tbgoods.setState(goods.getState());
        }
        if (goods.getNumber() != null){
            tbgoods.setNumber(goods.getNumber());
        }
        if (goods.getDescription() != null){
            tbgoods.setDescription(goods.getDescription());
        }
        return goodsMapper.updateByPrimaryKeySelective(tbgoods);

    }
    //删除商品
    @Override
    public  int deleteGoods(Integer id){
        return goodsMapper.deleteByPrimaryKey(id);
    }
    @Override
    //    满意度提交
    public int  feedBack(User user,Integer description){
        Feedback feedback = new Feedback();
        feedback.setUsername(user.getUsername());
        feedback.setDescription(description);
        return feedbackMapper.insertSelective(feedback);
    }
    @Override
    //投诉表提交
    public  int complaint(User user,String content){
        Complaint complaint = new Complaint();
        User tbuser = null;
        UserExample example = new UserExample();
        if (user != null && user.getId() != null) {
            example.createCriteria().andIdEqualTo(user.getId());
        }
        List<User> list = userMapper.selectByExample(example);
        if (list.size() > 0) {
            tbuser = list.get(0);
        }
        complaint.setUsername(tbuser.getUsername());
        complaint.setPhone(tbuser.getPhone());
        complaint.setContent(content);
        SimpleDateFormat dateFormat = new SimpleDateFormat();
        String date = dateFormat.format(new Date());
        complaint.setTime(date);
        //受理状态默认为0
        complaint.setHandle(0);
        return complaintMapper.insertSelective(complaint);
    }
//    @Override
//    //投诉表查询
//    public  List   complaintListHomePage(){
//        return  complaintMapper.selectByExample(null);
//
//    }
//    @Override
//    //投诉表点击页数查询
//    public Paging complaintListPage(Integer number){
//        ComplaintExample complaintExample = new ComplaintExample();
//        complaintExample.createCriteria().andIdBetween(6*(number-1),6*number);
//        List<Complaint> complaints = complaintMapper.selectByExample(complaintExample);
//
//
//        ComplaintExample complaintExample1 = new ComplaintExample();
//        complaintExample1.createCriteria().andIdIsNotNull();
//        List<Complaint> complaints1 = complaintMapper.selectByExample(complaintExample1);
//        Paging paging = new Paging();
//        paging.setList(complaints);
//        paging.setNumber(complaints1.size());
//        return  paging;
//    }
}

